import java.util.*;

public class My_TreeNode {
    static class TreeNode {//创建一个节点
        public char val;//存储节点的值
        public TreeNode left;//指向左孩子
        public TreeNode right;//指向右孩子

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public static TreeNode root;

    public static TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        TreeNode J = new TreeNode('J');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.left = H;
        D.right = J;
        return A;
    }

    //这是递归
    public static void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //非递归
    public static void preOrder1(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        if (root == null) {
            return;
        }
        stack.push(root);
        while (!stack.empty()) {
            TreeNode cur = stack.pop();
            if (cur.right != null) {
                stack.push(cur.right);
            }
            if (cur.left != null) {
                stack.push(cur.left);
            }
            System.out.print(cur.val + " ");
        }
        System.out.println();
    }

    //中序遍历
    //递归
    public static void inorder(TreeNode root) {
        //如果根节点为空，之间返回
        if (root == null) {
            return;
        }
        //中序遍历，是先左
        inorder(root.left);
        //打印左孩子的值
        System.out.print(root.val + " ");
        //再遍历右子树
        inorder(root.right);
    }

    //非递归
    public static void inorder1(TreeNode root) {
        Stack<TreeNode> s1 = new Stack<>();
        TreeNode cur = null;
        if (root != null) {
            cur = root;
            while (!s1.empty() || cur != null) {
                while (cur != null) {
                    s1.push(cur);
                    cur = cur.left;
                }
                if (!s1.empty()) {
                    cur = s1.pop();
                    System.out.print(cur.val + " ");
                    cur = cur.right;
                }
            }
        }
    }

    public static void lastOrder(TreeNode root) {
        //判断根节点
        if (root == null) {
            return;
        }
        //后续遍历，是左 右 根
        lastOrder(root.left);
        lastOrder(root.right);
        System.out.print(root.val + " ");
    }

    //非递归的
    public static void lastOrder1(TreeNode root) {
        Stack<TreeNode> s1 = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        if (cur != null) {
            while (!s1.empty() || cur != null) {
                while (cur != null) {
                    s1.push(cur);
                    cur = cur.left;
                }
                TreeNode top = s1.peek();
                if (top.right == null
                        || top.right == prev) {
                    System.out.print(top.val + " ");
                    s1.pop();
                    prev = top;
                } else {
                    cur = top.right;
                }
            }
        }
    }

    //层序遍历
    public void sequence(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> stack = new LinkedList<>();
        stack.offer(root);
        while (!stack.isEmpty()) {
            TreeNode cur = stack.poll();
            if (cur.left != null) {//左孩子不为空，入队
                stack.offer(cur.left);
            }
            if (cur.right != null) {//右孩子不为空，入队
                stack.offer(cur.right);
            }
            System.out.print(cur.val + " ");
        }
    }
    public  int findHeight(TreeNode root){
        //判断root是不是空节点
        if(root==null){
            return 0;
        }
        //递归问题，左右孩子树的高度
        int left=findHeight(root.left);
        int right=findHeight(root.right);
        //左右孩子谁高度比较高
        return left>right?left+1:right+1;
    }
    public  int getLevelNodeCount(TreeNode root,int k){
        //判断root是否为空，k是否合法
        if(root==null||k<0){
            return 0;
        }
        if(k==1){
            return 1;
        }
        //子问题，左右孩子第k层有多少节点
        int tmp=getLevelNodeCount(root.left,k-1)+
                getLevelNodeCount(root.right,k-1);
        return  tmp;
    }
    public int getLeafNode(TreeNode root){
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return  1;
        }
        int tmp=getLeafNode(root.left)+getLeafNode(root.right);
        return tmp;
    }
}